
// MemMapViewDoc.cpp : implementation of the CMemMapViewDoc class
//

#include "stdafx.h"
#include "MemMapView.h"

#include "MemMapViewDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMemMapViewDoc

IMPLEMENT_DYNCREATE(CMemMapViewDoc, CDocument)

BEGIN_MESSAGE_MAP(CMemMapViewDoc, CDocument)
	ON_COMMAND(ID_VIEW_REFRESH, &CMemMapViewDoc::OnViewRefresh)
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, &CMemMapViewDoc::OnUpdateFileSaveAs)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_THREADS, OnStatusIndicatorThreads)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_WORKINGSET, OnStatusIndicatorWorkingSet)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_PRIVATECOMMIT, OnStatusIndicatorPrivateCommit)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_TOTALCOMMIT, OnStatusIndicatorTotalCommit)
END_MESSAGE_MAP()


// CMemMapViewDoc construction/destruction

CMemMapViewDoc::CMemMapViewDoc()
{
	::ZeroMemory(&m_ProcessInfo, sizeof(m_ProcessInfo));
}

CMemMapViewDoc::~CMemMapViewDoc()
{
}

BOOL CMemMapViewDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}

// CMemMapViewDoc serialization

void CMemMapViewDoc::Serialize(CArchive& ar)
{
}


// CMemMapViewDoc diagnostics

#ifdef _DEBUG
void CMemMapViewDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMemMapViewDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CMemMapViewDoc commands

void CMemMapViewDoc::SetProcessInfo(PROCESSENTRY32* info) {
	m_ProcessInfo = *info;
	const PROCESSENTRY32* pe = GetProcessInfo();
	CString title;
	title.Format(_T("%s (%d)"), pe->szExeFile, pe->th32ProcessID);
	SetTitle(title);
	UpdateAllViews(NULL);
}

void CMemMapViewDoc::OnViewRefresh()
{
	UpdateAllViews(NULL);
}

void CMemMapViewDoc::OnUpdateFileSaveAs(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(FALSE);
}

void CMemMapViewDoc::OnStatusIndicatorThreads(CCmdUI* pCmdUI) {
	CString text;
	text.Format(_T("Threads: %d"), GetProcessInfo()->cntThreads);
	pCmdUI->Enable();
	pCmdUI->SetText(text);
}

void CMemMapViewDoc::OnStatusIndicatorWorkingSet(CCmdUI* pCmdUI) {
	CString text = _T("Accessed denied");
	HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetProcessID());
	if(hProcess) {
		PROCESS_MEMORY_COUNTERS counters = { sizeof(counters) };
		::GetProcessMemoryInfo(hProcess, &counters, sizeof(counters));
		text.Format(_T("Working Set: %d KB"), counters.WorkingSetSize >> 10);
		::CloseHandle(hProcess);
	}
	pCmdUI->Enable();
	pCmdUI->SetText(text);
}

void CMemMapViewDoc::SetCommitSizes(DWORD_PTR privateCommitted, DWORD_PTR totalCommitted) {
	m_PrivateCommitted = privateCommitted;
	m_TotalCommitted = totalCommitted;
}

void CMemMapViewDoc::OnStatusIndicatorTotalCommit(CCmdUI* pCmdUI) {
	CString text;
	text.Format(_T("Total Commit: %d KB"), m_TotalCommitted >> 10);
	pCmdUI->Enable();
	pCmdUI->SetText(text);
}

void CMemMapViewDoc::OnStatusIndicatorPrivateCommit(CCmdUI* pCmdUI) {
	CString text;
	text.Format(_T("Private Commit: %d KB"), m_PrivateCommitted>> 10);
	pCmdUI->Enable();
	pCmdUI->SetText(text);
}
